/*
 * Created by SharpDevelop.
 * User: Richi
 * Date: 13.03.2007
 * Time: 12:07
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using TagTool.Core;
using TagTool.Devices;
using TagTool.Devices.Keyboard;
using TagTool.GraphicTypes;

namespace TagTool.Nodes.Devices
{
	/// <summary>
	/// Description of KeyboardNode.
	/// </summary>
	public class KeyboardNode : BaseNode
	{
		
		public static IKeyboardDevice keyboard = WinKeyboard.Instance;

		// Key range. only keys inside the range are tracked.
		private InputSlot range;
		
		// The key invoked this event
		private OutputSlot key;
		 
		// keystate is pressed or released
		private OutputSlot pressed;
		
		// alt key was down while changing the keystate
		private OutputSlot altDown;
		
		public KeyboardNode() : base()
		{
			keyboard.KeyboardDeviceEvent += new KeyboardDeviceEventHandler(OnKeyChange);
			
			range = new InputSlot(this, "Range", new GraphicTypes.Range(0.0, 255.0));
			range.ParseCallback = GraphicTypes.Range.Parse;
			
			key = new OutputSlot(this, "Key", typeof(double));
			key.Value = 0.0;

			pressed = new OutputSlot(this, "Pressed", typeof(bool));
			pressed.Value = false;
			
			altDown = new OutputSlot(this, "AltDown", typeof(bool));
			altDown.Value = false;
			
			Name = "Keyboard";
		}

		public void OnKeyChange(Object sender, KeyboardDeviceEventArgs e)
		{
			if ( ((GraphicTypes.Range)range.Value).IsInside((double)e.Key))
			{
				key.Value = (double)e.Key;
				altDown.Value = e.AltDown;
				pressed.Value = e.Pressed;
				
				CastTrigger();
			}
		}
		
		public override void Update(Object sender)
		{ // do nothing to update keyboard events
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Range
		{
			get { return range; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Key
		{
			get { return key; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Pressed
		{
			get { return pressed; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot AltDown
		{
			get { return altDown; }
		}
		
	}
}
